/**
 * 模态框
 */
module.exports = function ($scope, $state, $resource, $stateParams, $modalInstance, $http, items, toaster) {

    $scope.week = [  
        {id : 1, name : '周一', checked: true},  
        {id : 2, name : '周二', checked: true},  
        {id : 3, name : '周三', checked: true},  
        {id : 4, name : '周四', checked: true},  
        {id : 5, name : '周五', checked: true},  
        {id : 6, name : '周六', checked: true},  
        {id : 0, name : '周日', checked: true}  
    ];  
    var today = new Date();
    $scope.today_end = (today.getFullYear()+1)+'-'+(today.getMonth()+1)+'-'+today.getDate();


    $scope.saleobj = {};
    $scope.tour = {};

    $scope.open = function ($event, item) {
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    };
    $scope.tour.start = {
        'date': {
            'label': date2str(today),
            'opened': false
        }
    };
    $scope.tour.end = {
        'date': {
            'label': date2str(today),
            'opened': false
        }
    };
    $scope.selectedDate = [];

    $scope.save = function () {
        var start_date = date2str($scope.tour.start.date.label);
        var end_date = date2str($scope.tour.end.date.label);
        $scope.selectedDate = dataScope(start_date, end_date);

        var result = [];
        for (var index = 0; index < $scope.selectedDate.length; index++) {
            var element = $scope.selectedDate[index];
            result.push({
                id: items.dateArray[element],
                date: element,
                sale_code: items.sale_code,
                market_price: $scope.saleobj.market_price,
                guide_price: $scope.saleobj.guide_price,
                cost_price: $scope.saleobj.cost_price,
                purchase_price: $scope.saleobj.purchase_price,
                current_stock_num: $scope.saleobj.current_stock_num
            });
        }

        $resource('/api/ac/tc/ticketSaleDayPriceService/createDayPrice', {}, {}).save({'daylist':result}, function (res) {
            if (res.errcode === 0) {
                toaster.success({ title: '', body: '保存成功' });
                $modalInstance.close({});
            } else {
                toaster.warning({ title: '', body: res.errmsg });
                $scope.lastResult = [];
            }
        });
    }


    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

    function date2str(d) {
        if (!d) {
            return "";
        }
        if(typeof d == 'string'){
            return d
        }
        var month = (d.getMonth() + 1).toString();
        var day = d.getDate().toString();
        if (month.length == 1) month = '0' + month;
        if (day.length == 1) day = '0' + day;
        return d.getFullYear() + "-" + month + "-" + day;
    }

    function str2date(strDate){
        var objDate = new Date(Date.parse(strDate.replace(/-/g, "/")));
        return objDate;
    }

    function dataScope(strdate1, strdate2){
        var date1 = str2date(strdate1);
        var date2 = str2date(strdate2);
        if (date1 > date2) {
            var tempDate = date1;
            date1 = date2;
            date2 = tempDate;
        }
        
        date2.setDate(date2.getDate() + 1);  //结束时间+1天，保证可以包含选中的最后一天
        var dateArr = [];
        while (!(date1.getFullYear() == date2.getFullYear()
            && date1.getMonth() == date2.getMonth()
            && date1.getDate() == date2.getDate())) {
            //当前日期的星期。
            var ddd = date1.getDay() - 1;
            if(ddd == -1){
                ddd = 6;
            }
            if($scope.week[ddd].checked){
                dateArr.push(date2str(date1));
            }
            date1.setDate(date1.getDate() + 1);
        }

        return dateArr;
    }

};